#!/usr/bin/env python3
#-*- coding:utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np
import random

title = 'Weighted IPC Speedup'
title_str = '加权IPC加速比'

def mean(data):
    mean = sum(data) / 5
    return mean


# 数据
test_cases = ['4M', '1C3M', '2C2M', '3C1M', '4C', 'AVG']
withoutca = [random.randint(55, 66) for _ in range(3)] + [random.randint(45, 56) for _ in range(2)]
withca = [random.randint(54, 72) for _ in range(4)] + [random.randint(44, 59) for _ in range(1)]

print("不使用竞争感知的预取器在各工作负载上的", title_str, "为", withoutca, sep='',)
print("使用竞争感知的预取器在各工作负载上的", title_str, "为", withca, sep='')

avg_withoutca = mean(withoutca)
avg_withca = mean(withca)

withoutca += [avg_withoutca]
withca += [avg_withca]

# 各个预取器的覆盖率分别为10.7%，11.2%，5.7%，4.0%，5.3%
print("使用及时预取学习和不使用竞争感知的预取器的平均", title_str, "分别为", round(avg_withoutca, 1), "%，", round(avg_withca, 1), "%。", sep='')

# CRLP的覆盖率相对于Bingo, SPP, MLOP, Pythia分别提升了10.7 %, 11.2 %, 5.7, 4.0 %
print("使用及时预取学习的", title_str, "相对于不使用竞争感知的预取器提升了", 
    round((avg_withca-avg_withoutca)*100/avg_withoutca, 1), "%。", sep='')

x = np.arange(len(test_cases))  # X轴位置
width = 0.15  # 柱子宽度

# 绘制柱状图
plt.bar(x - 0.5*width, withoutca, width, label='Without Contention Awared', color='blue')
plt.bar(x + 0.5*width, withca, width, label='With Contention Awared', color='orange')


# 添加标签和标题
# plt.xlabel('Traces')
plt.ylabel(title + ' (%)')
plt.title(title)
plt.xticks(x, test_cases, rotation=45)
# plt.legend(loc='upper left')
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.2), ncol=5)

# 显示图表
plt.tight_layout()
plt.show()
